<template>
    <div style="">
        <div class="container">
            <div class="handle-box">
                <el-select v-model="pId" placeholder="类别" class="handle-select mr10">
                    <el-option key="-1" label="全部" value="-1"></el-option>
                    <el-option v-for="item in modelList" :key="item.id" :label="item.value" :value="item.id"></el-option>
                </el-select>
                <el-input v-model="key" placeholder="typeid" class="handle-input mr10"></el-input>

                <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
            </div>
            <div style=" display: flex;justify-content: flex-end;margin-bottom: 20px;">
                <el-button type="primary" @click="addAccount">添加设备</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <el-table-column type="index" :index="methodIndex" label="序号" width="55" align="center"></el-table-column>
                <el-table-column prop="key" label="typeid"></el-table-column>
                <el-table-column label="类别" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.pId | modelFilter(modelList) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="添加时间" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.createTime | dateFilter }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="createUserName" label="添加账号"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" @click="handleCancle(scope.$index, scope.row)">移除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next,jumper"
                    :current-page="pageNo"
                    :page-size="pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 删除弹出框 -->
        <el-dialog class="popView" :visible.sync="isCancel" v-if="isCancel" :show-close="false" width="200px">
            <div class="main">
                <span class="title">请确认是否删除？</span>
                <div class="btns">
                    <el-button @click="isCancel = false">取 消</el-button>
                    <el-button type="primary" @click="actionCancle">删除</el-button>
                </div>
            </div>
        </el-dialog>
        <!-- 查看弹出框 -->
        <el-dialog class="popView" :visible.sync="isEdit" v-if="isEdit" :show-close="false" width="300px">
            <div class="main">
                <span class="title">{{ editTitle }}</span>
                <div class="item" style="margin-bottom: 10px;">
                    <span><span style="color:red">*</span> typeid：</span>
                    <el-input v-model="editMac" placeholder="请输入typeid" class="handle-input" style="width:150px"></el-input>
                </div>
                <div class="item" style="margin-bottom: 10px;">
                    <span><span style="color:red">*</span> 类别：</span>
                    <el-select v-model="editId" placeholder="请选择" style="width:150px">
                        <el-option v-for="item in modelList" :key="item.id" :label="item.value" :value="item.id"></el-option>
                    </el-select>
                </div>
                <div class="btns">
                    <el-button @click="isEdit = false">取 消</el-button>
                    <el-button type="primary" @click="finish">确定</el-button>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            tableData: [],
            multipleSelection: [],
            delList: [],

            pId: null,
            key: null,
            pageNo: 1,
            pageSize: 10,
            selItem: null,
            pageTotal: 0,
            isEdit: false,
            isCancel: false,
            modelList: [],
            editMac: '',
            editId: '',
            editTitle: '',
            id: null
        };
    },
    created() {
        this.getWhiteList();
        // this.getData();
    },
    filters: {
        modelFilter(val, list) {
            for (let i = 0; i < list.length; i++) {
                if (val == list[i].id) {
                    return list[i].value;
                }
            }
            return '--';
        }
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.$api
                .post({
                    url: '/whitelist/pageQuery',
                    data: {
                        pageNo: this.pageNo,
                        pageSize: this.pageSize,
                        param: {
                            key: this.key,
                            pId: this.pId != -1 ? this.pId : null,
                            pKey:'smartDeviceBlack_list'
                        }
                    }
                })
                .then(res => {
                    if (res.code && res.code == '200') {
                        this.tableData = res.data.result || [];
                        this.pageTotal = res.data.total;
                    }
                });
        },
        getWhiteList() {
            this.$api
                .get({
                    url: '/whitelist/getChildren',
                    data:{
                        key:'smartDeviceBlack_list'
                    }
                })
                .then(res => {
                    if (res.code && res.code == '200') {
                        console.log('🚀 ~ file: PortalWhiteList.vue ~ line 157 ~ getWhiteList ~ res', res);
                        this.modelList = res.data || [];
                        // this.pageTotal = res.data.total;
                        this.getData();
                    }
                });
        },
        // 触发搜索按钮
        handleSearch() {
            this.pageNo = 1;
            this.getData();
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.isEdit = true;
            this.editTitle = '编辑设备';
            this.editMac = row.key;
            this.editId = row.pId;
            this.id = row.id;
        },
        // 新增
        addAccount() {
            this.isEdit = true;
            this.editTitle = '修改设备';
            this.editMac = '';
            this.editId = '';
            this.id = null;
        },
        // 查看操作
        finish() {
            if (!this.editMac || this.editMac.length == 0) {
                this.$message.error('请输入mac地址');
                return;
            }
            if (!this.editId || this.editId.length == 0) {
                this.$message.error('请选择设备型号');
                return;
            }
            let self = this;
            self.$api
                .post({
                    url: '/whitelist/save',
                    data: {
                        pId: self.editId,
                        key: self.editMac,
                        id: self.id
                    }
                })
                .then(res => {
                    this.isEdit = false;
                    this.getData();
                })
                .catch(err => {});
        },
        // 删除操作
        handleCancle(index, row) {
            this.isCancel = true;
            this.selItem = row;
        },
        actionCancle() {
            this.$api
                .get({
                    url: '/whitelist/delById',
                    data: {
                        id: this.selItem.id
                    }
                })
                .then(res => {
                    this.isCancel = false;
                    this.$message.success('删除成功');
                    this.getData();
                });
        },

        // 分页导航
        handlePageChange(val) {
            this.pageNo = val;
            this.getData();
        },
        methodIndex(index) {
            return index + 1;
        }
    }
};
</script>

<style scoped lang="scss">
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 100px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
/deep/.popView {
    display: flex;
    align-items: center;
    justify-content: center;
    .el-dialog__header {
        padding: 0;
    }
    .el-dialog__body {
        padding: 0;
        border-radius: 10px;
    }
    .main {
        display: flex;
        flex-direction: column;
        align-items: center;
        background-color: #fff;
        border-radius: 10px;
        padding: 20px 0;
        .title {
            font-size: 17px;
            font-weight: bold;
            margin-bottom: 20px;
        }
        .btns {
            width: 100%;
            display: flex;
            justify-content: space-around;
        }
    }
}
</style>
